package com.jingxing.controller;

import com.jingxing.config.ApiResult;
import com.jingxing.config.UserLoginToken;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@Slf4j
@RestController
@RequestMapping("/worksInfo")
@Api(value = "API", tags = {"作品相关"})
@UserLoginToken
public class WorksInfoController {
    @Autowired
    private RestTemplate restTemplate;

    private static final String URL_20001 = "http://JINGXINGPROJECT-SERVER";

    @PostMapping("/saveWorksInfo")
    @ApiOperation("添加作品")
    public ApiResult savePlayerUserInfo(String playerId, Integer competitionLevel, String workName, String workUrl, String shareLinks) {
        MultiValueMap<String, Object> requestEntity = new LinkedMultiValueMap<>();
        requestEntity.add("playerId", playerId);
        requestEntity.add("competitionLevel", competitionLevel);
        requestEntity.add("workName", workName);
        requestEntity.add("workUrl", workUrl);
        requestEntity.add("shareLinks", shareLinks);
        return restTemplate.postForObject(URL_20001 + "/worksInfo/saveWorksInfo", requestEntity, ApiResult.class);
    }

    @PostMapping("/getWorksInfosByPlayerId")
    @ApiOperation("获取通过选手id获取选手作品")
    public ApiResult getWorksInfosByPlayerId(String playerId, Integer competitionLevel) {
        MultiValueMap<String, Object> requestEntity = new LinkedMultiValueMap<>();
        requestEntity.add("playerId", playerId);
        requestEntity.add("competitionLevel", competitionLevel);
        return restTemplate.postForObject(URL_20001 + "/worksInfo/getWorksInfosByPlayerId", requestEntity, ApiResult.class);
    }

    @PostMapping("/updateWorksInfo")
    @ApiOperation("修改作品分享链接")
    public ApiResult updateWorksInfo(String id, String shareLinks) {
        MultiValueMap<String, Object> requestEntity = new LinkedMultiValueMap<>();
        requestEntity.add("id", id);
        requestEntity.add("shareLinks", shareLinks);
        return restTemplate.postForObject(URL_20001 + "/worksInfo/updateWorksInfo", requestEntity, ApiResult.class);
    }

    @PostMapping("/deleteWorksInfo")
    @ApiOperation("删除作品")
    public ApiResult deleteWorksInfo(String id) {
        MultiValueMap<String, Object> requestEntity = new LinkedMultiValueMap<>();
        requestEntity.add("id", id);
        return restTemplate.postForObject(URL_20001 + "/worksInfo/deleteWorksInfo", requestEntity, ApiResult.class);
    }


}
